package eg.com.itiga.business.report;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import eg.com.itiga.business.base.BaseService;
import eg.com.itiga.business.exception.BusinessException;
import eg.com.itiga.business.factory.BusinessFactory;
import eg.com.itiga.domain.Company;
import eg.com.itiga.domain.Department;
import eg.com.itiga.domain.Itian;
import eg.com.itiga.domain.ItianCompany;
import eg.com.itiga.repository.base.BaseDao;
import eg.com.itiga.repository.company.CompanyDao;
import eg.com.itiga.repository.exception.DataException;
import eg.com.itiga.repository.factory.RepositoryFactory;
import eg.com.itiga.repository.itian.ItianDao;

public class ReportServiceImpl implements ReportService{


	public HashMap<String, Integer> getCompaniesAndItianWorkedFor()throws BusinessException {
		// TODO Auto-generated method stub
		try {	
			
			BaseService base = BusinessFactory.createBaseService();
			
			List list= base.findAll(Company.class);
			List Itianlist= base.findAll(Itian.class);
			
			HashMap<String, Integer> companiesList = new HashMap<String, Integer>();
			
			for(Object obj : list)
			{
				Company c=(Company)obj;
				companiesList.put(c.getCName(), new Integer(0)) ;	
			}
			BaseDao b;
			
			b = RepositoryFactory.createBaseDao();
			 
			b.beginTransaction();
			for(Object obj2 : Itianlist)
			{
				Itian itian=(Itian)obj2;
				
				Set companySet= itian.getItianCompanies();
				
				for(Object obj3 : companySet)
				{
					ItianCompany comp=(ItianCompany)obj3;
					
					String compName =comp.getCompany().getCName();
					if(companiesList.containsKey(compName))
					{
						Integer temp=companiesList.get(compName);
						companiesList.put(compName, new Integer(temp.intValue()+1));
					}
				}
			}
			b.commit();
			
			return companiesList;
			
			
			
			} catch (BusinessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
			catch (DataException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
	}


	public HashMap<String, List<Itian>> getUnWorkedByDept()throws BusinessException {
		try {
			BaseService base = BusinessFactory.createBaseService();
			ItianDao itianDao=RepositoryFactory.createItianDao();
			
			itianDao.beginTransaction();
			
			List deptList=itianDao.findAll(Department.class);
			
			HashMap<String, List<Itian>> resultMap=new HashMap<String, List<Itian>>();
			
			for(Object obj: deptList)
			{
				Department dept=(Department)obj;
				String deptName=dept.getDName();
				
				
				List<Itian> itianList= itianDao.getItianByDeptAndIntake(dept.getDId(), 30);
				
				itianList=  itianDao.getUnWorkedItians(itianList);
				
				resultMap.put(deptName, itianList);
			}
			
			return resultMap;
		
		} catch (BusinessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		catch (DataException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
	}


	public HashMap<String, Integer> getUnWorkedNOByDept() throws BusinessException {

		try
		{
			HashMap<String, Integer> resultMap=new HashMap<String, Integer>();
			HashMap<String, List<Itian>> tempMap=getUnWorkedByDept();
			for(String deptName : tempMap.keySet())
			{
				resultMap.put(deptName, tempMap.get(deptName).size());
			}
			return resultMap;
		}
		catch (Exception e) {
		
			e.printStackTrace();
			return null;
		}
	}

	public Hashtable<String, Integer> getItiansInACompany(int companyID) throws BusinessException 
	{
		Hashtable<String, Integer> list = new Hashtable<String, Integer>();
		
		CompanyDao companyDao = null;
		try 
		{
			companyDao = RepositoryFactory.createCompanyDao();
			companyDao.beginTransaction();
			
			Company company = (Company)companyDao.find(Company.class, companyID);
			
			Set itiansCompanies =  company.getItianCompanies();
			
			for (Iterator iterator = itiansCompanies.iterator(); iterator
					.hasNext();) {
				ItianCompany itianCompany = (ItianCompany) iterator.next();
				
				String deptName = itianCompany.getItian().getDepartment().getDName();
				Integer counter = 1;
				
				if(list.containsKey(deptName))
				{
					counter = list.get(deptName);
					counter++;
					
					list.remove(deptName);
				}
				
				list.put(deptName, counter);
			}
			
			companyDao.commit();
		} 
		catch (DataException e) {
			companyDao.rollback();
			e.printStackTrace();
		}
		return list ;
	}
	
	public List<Itian> getItiansListInACompany(int companyID) throws BusinessException
	{
		List<Itian> itiansList = new ArrayList<Itian>();
		
		CompanyDao companyDao = null;
		try 
		{
			companyDao = RepositoryFactory.createCompanyDao();
			companyDao.beginTransaction();
			
			Company company = (Company)companyDao.find(Company.class, companyID);
			
			Set itiansCompanies =  company.getItianCompanies();
			
			for (Iterator iterator = itiansCompanies.iterator(); iterator
					.hasNext();) {
				ItianCompany itianCompany = (ItianCompany) iterator.next();
				
				itiansList.add(itianCompany.getItian());				
			}
			
			companyDao.commit();
		} 
		catch (DataException e) {
			companyDao.rollback();
			e.printStackTrace();
		}
		return itiansList;
	}

}
